<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="员工姓名"/>
      </el-form-item>
      <el-form-item label="所属部门" prop="dept">
        <el-select v-model="dataForm.dept">
          <el-option v-for="dept in depts" :key="dept.value" :label="dept.label" :value="dept.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNum">
        <el-input v-model="dataForm.phoneNum" placeholder="员工手机号"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile } from '@/utils/validate'
  export default {
    data () {
      let validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        isLoading: false,
        depts: [
          {label: '客房部', value: '客房部'},
          {label: '销售部', value: '销售部'},
          {label: '餐饮部', value: '餐饮部'},
          {label: '晋南人家', value: '晋南人家'},
          {label: '工程部', value: '工程部'},
          {label: '财务部', value: '财务部'},
          {label: '保安部', value: '保安部'},
          {label: '办公室', value: '办公室'},
          {label: '人力资源及质检部', value: '人力资源及质检部'},
          {label: '退休人员', value: '退休人员'}
        ],
        dataForm: {
          id: 0,
          name: '',
          dept: '',
          phoneNum: '',
          qrCodeUrl: '',
          totalMember: '',
          createTime: '',
          isDel: '',
          def0: '',
          def1: ''
        },
        dataRule: {
          name: [
            {required: true, message: '员工姓名不能为空', trigger: 'blur'}
          ],
          dept: [
            {required: true, message: '所属部门不能为空', trigger: 'blur'}
          ],
          phoneNum: [
            {required: true, message: '员工手机号不能为空', trigger: 'blur'},
            {validator: validateMobile, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.isLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/unitstaff/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.unitStaff.name
                this.dataForm.dept = data.unitStaff.dept
                this.dataForm.phoneNum = data.unitStaff.phoneNum
                this.dataForm.qrCodeUrl = data.unitStaff.qrCodeUrl
                this.dataForm.totalMember = data.unitStaff.totalMember
                this.dataForm.createTime = data.unitStaff.createTime
                this.dataForm.isDel = data.unitStaff.isDel
                this.dataForm.def0 = data.unitStaff.def0
                this.dataForm.def1 = data.unitStaff.def1
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/unitstaff/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'dept': this.dataForm.dept,
                'phoneNum': this.dataForm.phoneNum,
                'qrCodeUrl': this.dataForm.qrCodeUrl,
                'totalMember': this.dataForm.totalMember,
                'createTime': this.dataForm.createTime,
                'isDel': this.dataForm.isDel,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.isLoadingVisible()
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      }
    }
  }
</script>
